package dimension.starry.player;

import dimension.starry.constants.PlayerDataType;
import dimension.starry.dataManager.PlayerDataManager;
import net.minecraft.server.network.ServerPlayerEntity;

import java.util.Arrays;

import static dimension.starry.constants.PlayerDataType.*;

public class AttrbuteService {

    public static void tryAddAttribute(ServerPlayerEntity serverPlayerEntity, String attributeName) {
        int attributePoints = PlayerDataManager.getInt(serverPlayerEntity, PlayerDataType.ATTRBUTE_POINTS);

        if (attributePoints <= 0) {
            return;
        }
        Arrays.stream(PlayerDataType.values())
            .filter(playerData -> playerData.getName().equals(attributeName))
            .filter(
                playerData -> STRENGTH.equals(playerData) || STRONG.equals(playerData) || AGILE.equals(playerData) ||
                    WISDOM.equals(playerData))
            .findAny()
            .ifPresent(playerData -> {
                int value = PlayerDataManager.getInt(serverPlayerEntity, playerData);
                PlayerDataManager.setServerPlayerData(serverPlayerEntity, playerData, value + 1);
                PlayerDataManager.setServerPlayerData(serverPlayerEntity, ATTRBUTE_POINTS, attributePoints - 1);
            });
    }

}
